<?php
	class InvPDF extends TCPDF
    {

        protected $invoice;
        protected $invoicedate;
        protected $custno;
        protected $invoicetype;
        public $letter = false;

        public function setMyHeaderData($invoice, $invoicedate, $custno)
        {
            $this->invoice = $invoice;
            $this->invoicedate = $invoicedate;
            $this->custno = $custno;
        }

        public function setInvoiceType($invoicetype)
        {
            $this->invoicetype = $invoicetype;
        }

        public function Header()
        {
            $rec = Rec::model()->find("invoice='$this->invoicedate'");
            $invoice = Invoice::model()->findByPk($this->invoicedate);

            if ($invoice->invoicetype == Invoice::TYPE_INVOICE && $invoice->checkPaid())
                $this->Image(K_PATH_IMAGES . 'paid.jpg', 75, 50, 25, 24);
            if ($rec)
            {
                $balance = $rec->invamt - $rec->paid;


                if ($balance == 0 && $rec->invamt > 0)
                {
                    $this->Image(K_PATH_IMAGES . 'paid.jpg', 75, 50, 25, 24);
                }
            }

            $this->y = $this->header_margin;
            $this->x = $this->original_lMargin;
            //$this->Image($file,$x,$y,$w,$h);
            $this->RoundedRect(17, 16.3, 87, 26, 6.50, '1111');
            $this->Image(K_PATH_IMAGES . 'QSI1.png', 23, 17, 25, 24);
            $this->MultiCell(40, 4, 'QSI, INC', false, 'C', false, 1, 65, 17);
            $this->MultiCell(40, 4, '107 Schuler Dr.', false, 'C', false, 1, 63, 22);
            $this->MultiCell(40, 4, 'Bardstown KY 40004', false, 'C', false, 1, 63, 27);
            $this->MultiCell(40, 4, 'Phone: 502-350-1000 ', false, 'C', false, 1, 63, 32);
            $this->MultiCell(40, 4, 'Fax: 502-350-1002 ', false, 'C', false, 1, 63, 37);
            switch ($this->invoicetype)
            {
                case Invoice::TYPE_CREDIT_MEMO:
                    $this->RoundedRect(106.1, 16.3, 87.8, 13, 1.50, '1111', 'DF', array(), array(255, 255, 0));
                    $this->writeHTMLCell(87.8, 20, 106.1, 20, $html = '<h1 style="text-align: center">CREDIT MEMO</h1>');
                    $label = 'Memo No';
                    break;
                case Invoice::TYPE_SALES_ORDERS:
                    if (isset($_REQUEST['list']))
                    {
                        $this->RoundedRect(106.1, 16.3, 87.8, 13, 1.50, '1111', 'DF', array(), array(153, 102, 153));
                        $this->writeHTMLCell(87.8, 20, 106.1, 20, $html = '<h1 style="text-align: center">PACKING LIST</h1>');
                    }
                    else
                    {
                        $this->RoundedRect(106.1, 16.3, 87.8, 13, 1.50, '1111', 'DF', array(), array(255, 255, 0));
                        $this->writeHTMLCell(87.8, 20, 106.1, 20, $html = '<h1 style="text-align: center">SALES ORDER</h1>');
                    }
                    $label = 'Sales Order No';
                    break;
                case Invoice::TYPE_QUOTE:

                    $this->RoundedRect(106.1, 16.3, 87.8, 13, 1.50, '1111', 'DF', array(), array(255, 0, 0));
                    $this->writeHTMLCell(87.8, 20, 106.1, 20, $html = '<h1 style="text-align: center">QUOTE</h1>');
                    $label = 'Quote No';
                    break;
                case Invoice::TYPE_INVOICE:
                    $this->RoundedRect(106.1, 16.3, 87.8, 13, 1.50, '1111', 'DF', array(), array(51, 204, 0));
                    $this->writeHTMLCell(87.8, 20, 106.1, 20, $html = '<h1 style="text-align: center">INVOICE</h1>');
                    $label = 'Invoice No';

                    break;
                case 'PO':
                    //$this->Image(K_PATH_IMAGES . 'invoicelogo.png', 110, 15, 80, 15);
                    break;
                default :
                    $this->RoundedRect(106.1, 16.3, 87.8, 13, 1.50, '1111', 'DF', array(), array(51, 204, 0));
                    $this->writeHTMLCell(87.8, 20, 106.1, 20, $html = '<h1 style="text-align: center">INVOICE</h1>');
                    $label = 'Invoice No';
                    break;
            }


            $html = '                <br/><br/><br/><br/>
<table border="0" cellspacing="10" cellpadding="10"  >
     <tr>

        <th style="width:52%;" align="right" colspan = "2" >

        </th>
        <th  colspan = "2">

              <table cellspacing="1" cellpadding="2" >
                <tr>
                <h2></h2> <br><br>
                    <td align="center" style="background-color: #dddddd;border: 1px solid black;" >Date</td>

                    <td  align="center" style="background-color: #dddddd;border: 1px solid black;">
                    ';


            $html .= $label . '.</td>
                    <td  align="center" style="background-color: #dddddd;border: 1px solid black;">Cust No.</td>

                </tr>

                <tr>
                <td align="center" style="border: 1px double black;" ><b>' . $this->invoice . '</b></td>
                    <td align="center" style="border: 1px double black;" ><b>' . $this->invoicedate . '</b></td>

                    <td align="center" style="border: 1px double black;" ><b>' . $this->custno . '</b></td>
                </tr>
                  </table>

        </th>
    </tr>
    </table>
                ';
            $this->writeHTMLCell(200.5, 60, 0, 0, $html);
            if ($this->letter)
            {

                if ($this->getPage() == 2)
                {
                    $this->Rect(15, 90, 180, 185, 'D', array('width' => 2));
                }
                else
                {
                    if ($this->getPage() != 1)
                        $this->Rect(15, 50, 180, 235, 'D', array('width' => 2));
                }
            }else
            {
                if ($this->getPage() == 1)
                {
                    $this->Rect(15, 94, 186, 180, 'D', array('width' => 2));
                }
                else
                {
                    $this->Rect(15, 50, 186, 218, 'D', array('width' => 2));
                }
            }
        }

        public function Footer()
        {
            $this->SetX($this->original_rMargin);

            $pagetext = 'Page ' . $this->getAliasNumPage() . '/' . $this->getAliasNbPages();

            $this->MultiCell(80, 5, 'www.qsibanking.com', 0, 'L', 0, 0, '', '', true);
            $this->MultiCell(45, 5, $pagetext, 0, 'L', 0, 0, '', '', true);
            $this->MultiCell(55, 5, 'billing@qsibanking.com ', 0, 'R', 0, 1, '', '', true);
        }
		public function checkPageBreakHeight($h=0,$y='',$addPage=true)
		{
			return $this->checkPageBreak($h,$y,$addPage);
		}
    }